package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.kubernetes.models.RoleBindingRequestData;
import com.ruoyi.kubernetes.service.IKubernetesClientRoleBindingService;
import com.ruoyi.web.utils.KubernetesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.KubernetesGroupRoleBindingMapper;
import com.ruoyi.web.domain.KubernetesGroupRoleBinding;
import com.ruoyi.web.service.IKubernetesGroupRoleBindingService;

/**
 * K8s组权限Service业务层处理
 *
 * @author xuchengchao
 * @date 2022-04-17
 */
@Service
public class KubernetesGroupRoleBindingServiceImpl implements IKubernetesGroupRoleBindingService
{
    @Autowired
    private KubernetesGroupRoleBindingMapper kubernetesGroupRoleBindingMapper;

    @Autowired
    private IKubernetesClientRoleBindingService kubernetesClientRoleBindingService;

    /**
     * 查询K8s组权限
     *
     * @param id K8s组权限主键
     * @return K8s组权限
     */
    @Override
    public KubernetesGroupRoleBinding selectKubernetesGroupRoleBindingById(Long id)
    {
        return kubernetesGroupRoleBindingMapper.selectKubernetesGroupRoleBindingById(id);
    }

    /**
     * 查询K8s组权限列表
     *
     * @param kubernetesGroupRoleBinding K8s组权限
     * @return K8s组权限
     */
    @Override
    public List<KubernetesGroupRoleBinding> selectKubernetesGroupRoleBindingList(KubernetesGroupRoleBinding kubernetesGroupRoleBinding)
    {
        return kubernetesGroupRoleBindingMapper.selectKubernetesGroupRoleBindingList(kubernetesGroupRoleBinding);
    }

    /**
     * 新增K8s组权限
     *
     * @param kubernetesGroupRoleBinding K8s组权限
     * @return 结果
     */
    @Override
    public int insertKubernetesGroupRoleBinding(KubernetesGroupRoleBinding kubernetesGroupRoleBinding)
    {
        RoleBindingRequestData roleBindingRequestData = KubernetesUtils.getRoleBindingRequestData(kubernetesGroupRoleBinding);
        if(kubernetesClientRoleBindingService.addBinding(roleBindingRequestData)) {
            return kubernetesGroupRoleBindingMapper.insertKubernetesGroupRoleBinding(kubernetesGroupRoleBinding);
        }else {
            return 0;
        }
    }

    /**
     * 修改K8s组权限
     *
     * @param kubernetesGroupRoleBinding K8s组权限
     * @return 结果
     */
    @Override
    public int updateKubernetesGroupRoleBinding(KubernetesGroupRoleBinding kubernetesGroupRoleBinding)
    {
        return kubernetesGroupRoleBindingMapper.updateKubernetesGroupRoleBinding(kubernetesGroupRoleBinding);
    }

    /**
     * 批量删除K8s组权限
     *
     * @param ids 需要删除的K8s组权限主键
     * @return 结果
     */
    @Override
    public int deleteKubernetesGroupRoleBindingByIds(Long[] ids)
    {
        if (ids.length==1){
            RoleBindingRequestData roleBindingRequest = KubernetesUtils.getRoleBindingRequestData(kubernetesGroupRoleBindingMapper.selectKubernetesGroupRoleBindingById(ids[0]));
            if (kubernetesClientRoleBindingService.deleteBinding(roleBindingRequest)) {
                return kubernetesGroupRoleBindingMapper.deleteKubernetesGroupRoleBindingById(ids[0]);
            }
        }
        return kubernetesGroupRoleBindingMapper.deleteKubernetesGroupRoleBindingByIds(ids);
    }

    /**
     * 删除K8s组权限信息
     *
     * @param id K8s组权限主键
     * @return 结果
     */
    @Override
    public int deleteKubernetesGroupRoleBindingById(Long id)
    {
        RoleBindingRequestData roleBindingRequest = KubernetesUtils.getRoleBindingRequestData(kubernetesGroupRoleBindingMapper.selectKubernetesGroupRoleBindingById(id));
        if (kubernetesClientRoleBindingService.deleteBinding(roleBindingRequest)) {
            return kubernetesGroupRoleBindingMapper.deleteKubernetesGroupRoleBindingById(id);
        }else {
            return 0;
        }
    }
}
